package jsu.xnm.evelyn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jsu.xnm.evelyn.bean.Employee;
import jsu.xnm.evelyn.service.EmployeeService;
import jsu.xnm.evelyn.util.BaseContext;
import jsu.xnm.evelyn.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * @author xnm
 * @version 1.0
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    /**
     * 1.处理employee登录方法
     * @param employee 员工信息
     * @return Result对象
     */
    @PostMapping("/login")
    public Result login(@RequestBody Employee employee, HttpServletRequest req){
        System.out.println("登录");
        //1.将提交的用户密码进行md5加密
        String password = employee.getPassword();
//        password = DigestUtils.md5DigestAsHex(password.getBytes());//前端配置了md5加密，这里就不需要了

        //2.根据用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());

        Employee emp = employeeService.getOne(queryWrapper);//根据条件查询一条数据

        if(emp == null){//用户比对
            return Result.error("400","用户名不存在");
        }
        if(!emp.getPassword().equals(password)){//密码比对
            return Result.error("400","密码错误");
        }
        if(emp.getStatus() == 0){//账号状态比对
            return Result.error("400","账号已禁用");
        }
        //登录成功，将员工id存入session
        req.getSession().setAttribute("employee",emp.getId());
        //因为是前后端分离 前端请求不带session所以后面无法获取到session//todo
        return Result.success(emp);
    }

    /**
     * 处理新增员工
     * @param employee 员工信息
     * @param req 获取添加或修改该员工信息的id
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Employee employee,HttpServletRequest req){
        Integer cuId = (Integer)req.getSession().getAttribute("employee");//登录之后才有的逻辑 todo 用token替换
        BaseContext.setCurrentId(cuId);
        employeeService.saveEmployee(employee,4);
        return Result.success("新增员工成功");
    }

    @GetMapping("/page")
    public Result page(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "search",required = false,defaultValue = "") String search){
        System.out.println("pageNo:"+pageNo+"pageSize:"+pageSize+"search:"+search);
        //1.分页查询
        LambdaQueryWrapper<Employee> queryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.hasText(search)){
            queryWrapper.like(Employee::getName,search);
        }
        Page<Employee> page = employeeService.page(new Page<>(pageNo, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 处理employee修改方法
     * 修改员工账号状态和员工信息 合二为一
     * @param employee 员工信息
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee, HttpServletRequest req){
        log.info("employee:{}",employee);
        Integer empId = (Integer)req.getSession().getAttribute("employee");
        System.out.println("empId="+empId);
        //todo
        BaseContext.setCurrentId(4);

        employeeService.updateById(employee);

        return Result.success("修改员工信息成功");
    }

    //修改用户前的数据回显
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id){
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return Result.success(employee);
        }
        return Result.error("400","没有该员工信息");
    }

    /**
     * 2.处理employee退出方法
     * @param req request请求
     * @return Result对象
     */
    @PostMapping("/logout")
    public Result logout(HttpServletRequest req){
        req.getSession().removeAttribute("employee");
        return Result.success("退出成功!");
    }
}
